﻿<%@ Page MasterPageFile="~/Site.Master" Language="C#" AutoEventWireup="true" CodeBehind="OrderMake.aspx.cs" Inherits="RabbitShop.UI.OrderMake" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    
    <form id="form1" action="/Action/OrderAdd.aspx" method="POST">
        
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>

    <script type="text/javascript">
        
        var json = <%=CompanyJson%>;
        var index = <%=Index%>;
        
        function cmbSelect(cmb, str) {
            for (var i = 0; i < cmb.options.length; i++) {
                if (cmb.options[i].value == str) {
                    cmb.selectedIndex = i;
                    return;
                }
            }
        }
        function cmbAddOption(cmb, str, val, chk) {
            var option = document.createElement("OPTION");
            cmb.options.add(option);
            option.innerHTML = str;
            option.value = val;
            if (chk) {
                option.selected = true;
            }
        }

        function initCompany() {
            var cmb = document.getElementById("dilivery");
            for (var i = 0; i < json.length; i++) {
                var check = false;
                if (index == i) {
                    check = true;
                }
                cmbAddOption(cmb, json[i], i, check);
            }
            cmb.select = index;
        }
        
        function selectAddress(index) {
            var xhr = new XMLHttpRequest();
            xhr.open("post", "/Action/OrderAddress.aspx?aid=" + index, true);
            xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

            xhr.send();
            xhr.onloadend = function () {
                if (xhr.responseText == "true") {
                    window.location = "OrderMake.aspx";
                } else {
                    alert(xhr.responseText);
                }
            };
        }
        
        function selectDiliveryCompany() {
            var select = document.getElementById("dilivery");
            var xhr = new XMLHttpRequest();
            xhr.open("post", "/Action/OrderShipCompany.aspx?aid=" + select.value, true);
            xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

            xhr.send();
            xhr.onloadend = function () {
                if (xhr.responseText == "true") {
                    window.location = "OrderMake.aspx";
                } else {
                    alert(xhr.responseText);
                }
            };
        }
        
        function getCookie(c_name) {
            if (document.cookie.length > 0) {
                c_start = document.cookie.indexOf(c_name + "=");
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1;
                    c_end = document.cookie.indexOf(";", c_start);
                    if (c_end == -1) c_end = document.cookie.length;
                    return unescape(document.cookie.substring(c_start, c_end));
                }
            }
            return "";
        }
        
        function SubmitOrder() {
            var uid = getCookie("uid");
            if (uid == "") {
                window.location = "UserLogon.aspx";
                return;
            }

            var xhr = new XMLHttpRequest();
            xhr.open(form1.method, form1.action, true);
            xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

            xhr.send();

            xhr.onloadend = function () {
                if (xhr.responseText == "true") {
                    window.location = "OrderList.aspx";
                } else {
                    alert(xhr.responseText);
                }
            };
        }
    </script>
    
    <section class="contact">
        <header>
            <h3>Order:</h3>
        </header>

        
        <table>
    
            <asp:Repeater runat="server" ID="addressRepeater" >
                <ItemTemplate>
                <tr>
                    <td>
                        <input type="radio" id="address<%#Container.ItemIndex %>" name="selectedAddressIndex" onclick="selectAddress(<%#Container.ItemIndex%>);"  />
                    </td>
                                
                    <td>
                        <%#Eval("Province") %>
                    </td>
                    <td>
                        <%#Eval("City") %>
                    </td>
                    <td>
                        <%#Eval("Region") %>
                    </td>
                    <td>
                        <%#Eval("Detail") %>
                    </td>
                            
                    <td>
                        <%#Eval("ContactName") %>
                    </td>
                    <td>
                        <%#Eval("ContactPhone") %>
                    </td>
                    <td>
                        <%#Eval("PostalCode") %>
                    </td>

                </tr>
                </ItemTemplate>
            </asp:Repeater>
                
        </table>
            
        <br/>
        <a href="/UI/ProductList.aspx"><%=ShopInfo.NickName %></a>

        <br/>
        <table style="text-align: center;">
            <tr>
                <td>商品</td>
                <td>名称</td>
                <td>标题</td>
                <td>单价</td>
                <td>数量</td>
                <td>小计</td>
                <td>操作</td>
            </tr>

    <asp:Repeater runat="server" ID="productRepeater" >
        <ItemTemplate>
            <tr>
                <td>
                    <a href="ProductDetail.aspx?id=<%#Eval("ProductInfo.Id") %>" target="_blank" >
                        <img alt="产品图片" width="50" src="../Images/accent.png" />
                    </a>
                </td>
                <td style="width: 50px;">
                    <%#Eval("ProductInfo.Name") %>
                </td>
                <td style="width: 300px;">
                    <div>
                        <%#Eval("ProductInfo.Title") %>
                    </div>
                    <br/>
                    <div>
                        <%#Eval("ProductInfo.SubTitle") %>
                    </div>
                </td>
                <td>
                    <%#Eval("ProductInfo.PricePerUnit") %>
                </td>
                <td>
                    <a onclick="ChangeQuantity('sub','quantity<%#Eval("ProductInfo.Id") %>');" href="javascript:void(0);">-</a>
                    <input type="text" value="<%#Eval("Quantity") %>" id='quantity<%#Eval("ProductInfo.Id") %>' style="width: 30px; text-align: right;"/>
                    <a onclick="ChangeQuantity('add','quantity<%#Eval("ProductInfo.Id") %>');" href="javascript:void(0);">+</a> 
                                
                </td>
                <td>
                    <%#(decimal)Eval("ProductInfo.PricePerUnit") * (int)Eval("Quantity") %>
                </td>
                <td>
                    <a href="/Action/CartItemDelete.aspx?oid=<%#Eval("ProductInfo.Id") %>">删除</a>
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
        </table>

            
        <div>
            <div style="float: right;">
                <table style="text-align: right;">
                    <tr>
                        <td style="height: 30px;">数量总计：</td>
                        <td><%=CartTotal.TotalNumber %></td>
                        <td style="text-align: left;">件</td>
                    </tr>
                    <tr>
                        <td style="height: 30px;">重量总计：</td>
                        <td><%=CartTotal.TotalWeight %></td>
                        <td>kg</td>
                    </tr>
                    <tr>
                        <td style="height: 30px;"><strong>商品总价</strong>(不含运费)：</td>
                        <td><span>￥<strong id="Strong1"><%=CartTotal.TotalPrice %></strong></span></td>
                        <td>元</td>
                    </tr>
                    <tr>
                        <td style="height: 30px;">
                             <% if (IsNear) { %><select id="dilivery" onchange="selectDiliveryCompany()"></select><% } %>
                            <strong><%=DiliveryPrice.Reason %></strong>：
                        </td>
                        <td><span>￥<strong id="Strong2"><%=DiliveryPrice.Price %></strong></span></td>
                        <td>元</td>
                    </tr>
                    
                    <tr>
                        <td style="height: 30px;"><strong>总价</strong>：</td>
                        <td><span>￥<strong id="Strong3"><%=DiliveryPrice.Price+CartTotal.TotalPrice %></strong></span></td>
                        <td>元</td>
                    </tr>
                </table>
                
                <div style="float: right; width: 173px;background: #f40 none repeat scroll 0 0;
border-color: #f40;

border-radius: 2px;
border-style: solid;
border-width: 1px;
cursor: pointer;
display: block;
font-family: Hiragino Sans GB,microsoft yahei,sans-serif;
font-size: 16px;
line-height: 38px;
text-align: center;">
            <a class="J_LinkAdd"  href="javascript:SubmitOrder();"
                style="color: #fff;" >
                <i class="tb-iconfont" style=" font-size: 18px;
font-style: normal;
font-weight: 400;
margin-right: 10px;
vertical-align: bottom;">ŭ</i>
                        确认下单
                    </a>
                </div>
            </div>
        </div>
        
    </section>
        
<script type="text/javascript">
    function check() {
        document.getElementById("address<%=SelectedIndex%>").checked = true;
    }

    $(document).ready(check());
    $(document).ready(initCompany());
</script>

    </form>
</asp:Content>
